package com.lianjia.sh.tools.msg.v1.sender.impl;

import com.lianjia.sh.tools.msg.v1.model.MailCustomerMsg;
import com.lianjia.sh.tools.msg.v1.model.MailMsg;
import com.lianjia.sh.tools.msg.v1.model.Msg;
import com.lianjia.sh.tools.msg.v1.sender.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.Assert;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by yangsp1 on 16/6/28.
 */
public class MailSenderImpl implements MailSender {
    JavaMailSenderImpl defaultMailSender;
    /**
     * 默认发送邮箱服务器
     */
    String defaultHost;
    /**
     * 默认发送邮箱
     */
    String defaultFrom;

    /**
     * 默认发送邮箱密码
     */
    String defaultPassword;

    /**
     * 默认超时时间
     */
    Long defaultTimeout = 5000L;


    @Override
    public boolean isMatch(Msg msg) {
        return msg != null && msg instanceof MailMsg;
    }

    @Override
    public boolean send(Msg msg) {
        Assert.notNull(msg);
        if(msg instanceof MailCustomerMsg){
            return send((MailCustomerMsg)msg);
        }else if(msg instanceof MailMsg){
           return send((MailMsg)msg);
        }
        return true;
    }


    public boolean send(MailMsg mailMsg){
        JavaMailSenderImpl mailSender = instanceDefaultSender();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setSubject(mailMsg.getSubject());
            messageHelper.setFrom(defaultFrom);
            messageHelper.setTo(mailMsg.getTo());
            messageHelper.setText(mailMsg.getText(), true);
            mailSender.send(mimeMessage);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
        return true;
    }

    public boolean send(MailCustomerMsg mailCustomerMsg){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(mailCustomerMsg.getHost());
        mailSender.setUsername(mailCustomerMsg.getUserName());
        mailSender.setPassword(mailCustomerMsg.getPassword());
        Properties prop = getDefaultCfg();
        mailSender.setJavaMailProperties(prop);

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setSubject(mailCustomerMsg.getSubject());
            messageHelper.setFrom(mailCustomerMsg.getFrom());
            messageHelper.setTo(mailCustomerMsg.getTo());
            messageHelper.setText(mailCustomerMsg.getText(), true);
            mailSender.send(mimeMessage);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
        return true;
    }



    public JavaMailSenderImpl instanceDefaultSender()
    {
        if (defaultMailSender != null) {
            return defaultMailSender;
        }

        defaultMailSender = new JavaMailSenderImpl();
        defaultMailSender.setHost(defaultHost);
        defaultMailSender.setUsername(defaultFrom);
        defaultMailSender.setPassword(defaultPassword);
        Properties prop = getDefaultCfg();
        defaultMailSender.setJavaMailProperties(prop);
        return defaultMailSender;
    }

    public Properties getDefaultCfg(){
        Properties prop = new Properties();
        //需要认证
        prop.put("mail.smtp.auth", "true");
        //超时
        prop.put("mail.smtp.timeout", defaultTimeout.toString());

        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.socketFactory.port", "465");
        prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.put("mail.smtp.socketFactory.fallback", "false");
        return prop;
    }

    public String getDefaultHost() {
        return defaultHost;
    }

    public void setDefaultHost(String defaultHost) {
        this.defaultHost = defaultHost;
    }

    public String getDefaultFrom() {
        return defaultFrom;
    }

    public void setDefaultFrom(String defaultFrom) {
        this.defaultFrom = defaultFrom;
    }

    public String getDefaultPassword() {
        return defaultPassword;
    }

    public void setDefaultPassword(String defaultPassword) {
        this.defaultPassword = defaultPassword;
    }

    @Override
    public Long getDefaultTimeout() {
        return defaultTimeout;
    }

    @Override
    public void setDefaultTimeout(Long defaultTimeout) {
        this.defaultTimeout = defaultTimeout;
    }
}
